package com.hyyt.shopjsp.basic.controller;

import com.hyyt.shopjsp.basic.pojo.BasicActor;
import com.hyyt.shopjsp.basic.service.IBasicActorService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Zhang Dingfang on 2016/10/26 0026下午 6:48.
 */
@Controller
@RequestMapping("/back/actor")
public class BasicActorController extends BaseController {

    private Logger log = Logger.getLogger(BasicActorController.class);
    private String basePath = "/back/basic/actor/";

    @Resource
    private IBasicActorService basicActorService;

    /**
     * 进入角色页面
     *
     * @return 进入指定页面
     */
    @RequestMapping("gotoActorPage")
    public String gotoActorPage() {
        return basePath + "actorList";
    }

    /**
     * 保存或者修改角色
     * @param actor
     * @return
     */
    @RequestMapping(value = "saveOrUpdateActor", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdateActor(BasicActor actor) {
        if(Utils.objectIsNotEmpty(actor)){
            if (Utils.objectIsEmpty(actor.getActorId())) {
                basicActorService.saveBasicActor(actor);
            }else {
                basicActorService.updateBasicActor(actor);
            }
            return ResultMsg.create(true);
        }else{
            return ResultMsg.create(false);
        }

    }

    /**
     * 获得角色对象
     * @param actorId
     * @return
     */
    @RequestMapping(value = "getActorObject", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getActorObject(Integer actorId) {
        if (Utils.objectIsNotEmpty(actorId)) {
            BasicActor actor = basicActorService.loadBasicActor(actorId);
            return ResultMsg.create(true, null, FastJsonUtils.toJSONString(actor));
        }else {
            return ResultMsg.create(false);
        }
    }

    /**
     * 删除指定角色对象
     * @param actorId 删除角色的id，多个以 , 隔开
     * @return
     */
    @RequestMapping(value = "deleteActor", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteActor(Integer actorId) {
        if (Utils.objectIsNotEmpty(actorId)) {
            basicActorService.deleteBasicActor(actorId);
            return ResultMsg.create(true);
        }else{
            return ResultMsg.create(false);
        }
    }

    /**
     * 查询角色集合
     * @param response
     */
    @RequestMapping(value = "listActor", method = RequestMethod.POST)
    @ResponseBody
    public void listActor(String actorName, Page page, HttpServletResponse response) {
        Map<String, Object> param = new HashMap<>();
        if (actorName!=null&&!"".equals(actorName.trim())) {
            param.put("actorName", actorName);
        }
        param.put("sortColumns", "actorId desc");
        int totalRecordCount = basicActorService.countBasicActor(param);
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        List<BasicActor> list = basicActorService.queryBasicActor(param, page.getPageRecordBeginIndex(), page.getPageSize());
        param.clear();
        param.put("total", totalRecordCount);// total键 存放总记录数，必须的
        param.put("rows", list);// rows键 存放每页记录 list
        ResultMsg.create(param, response);  //把内容发送到页面
    }
}